package controller

import (
	"60321/pkg/global"
	"60321/pkg/models"
	"60321/pkg/models/requests"
	"github.com/gin-gonic/gin"
	"net/http"
)

// AddCitiesInDB 把城市数据添加到数据库中
func AddCitiesInDB(context *gin.Context) {
	var request requests.AddCitiesRequest
	if err := context.ShouldBindJSON(&request); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var city = models.CityModel{
		CityName:             request.CityName,
		UrbanCharacteristics: request.UrbanCharacteristics,
	}
	if err := global.Db.Create(&city).Error; err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	context.JSON(http.StatusCreated, gin.H{"city": city})
}

// AddTaxisInDB 把出租车的数据添加到数据库中
func AddTaxisInDB(context *gin.Context) {
	var request requests.AddTaxisRequest
	if err := context.ShouldBindJSON(&request); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var taxi = models.TaxiModel{
		CarOwnerName:  request.CarOwnerName,
		Price:         request.Price,
		DepartureTime: request.DepartureTime,
	}
	var city = models.CityModel{}
	if err := global.Db.Take(&city, "city_name = ?", request.CityName).Error; err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	if err := global.Db.Create(&taxi).Error; err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	var cityTaxi = models.CityTaxiModel{
		CityID:    city.ID,
		TaxiID:    taxi.ID,
		TaxiModel: taxi,
		CityModel: city,
	}
	// 把出租车添加到这个中间表中
	if err := global.Db.Create(&cityTaxi).Error; err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	context.JSON(http.StatusCreated, gin.H{"taxi": taxi})
}

// AddTicketsInDB 添加车票数据到数据库中
func AddTicketsInDB(context *gin.Context) {
	var request requests.AddTicketsRequest
	if err := context.ShouldBindJSON(&request); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var ticket = models.TicketModel{
		Origin:           request.Origin,
		Destination:      request.Destination,
		PassCityNum:      request.PassCityNum,
		Price:            request.Price,
		DepartureTime:    request.DepartureTime,
		TravelMode:       request.TravelMode,
		SeatType:         request.SeatType,
		IssuanceQuantity: request.IssuanceQuantity,
		ArrivalTime:      request.ArrivalTime,
		Vehicle:          request.Vehicle,
	}
	if err := global.Db.Create(&ticket).Error; err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	context.JSON(http.StatusCreated, gin.H{"ticket": ticket})
}

// GetTaxis 获得所有出租车司机的信息
func GetTaxis(context *gin.Context) {
	var taxis []models.TaxiModel
	if err := global.Db.Find(&taxis).Error; err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	context.JSON(http.StatusOK, gin.H{"taxis": taxis})
}

func AddCityAdjacent(context *gin.Context) {
	var request requests.ConnectCityRequest
	var mainCity = models.CityModel{}
	if err := context.ShouldBindJSON(&request); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	global.Db.Take(&mainCity, request.MainCityID)
	var adjacentCities []models.CityModel
	if err := global.Db.Take(&adjacentCities, request.AdjacentCityIDList).Error; err != nil {
		context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	for _, city := range adjacentCities {
		var adjacentCity = models.AdjacentCityModel{
			MainCityID:        mainCity.ID,
			MainCityName:      mainCity.CityName,
			MainCityModel:     mainCity,
			AdjacentCityID:    city.ID,
			AdjacentCityName:  city.CityName,
			AdjacentCityModel: city,
		}
		if err := global.Db.Create(&adjacentCity).Error; err != nil {
			context.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}
	}
	context.JSON(http.StatusCreated, gin.H{"city": mainCity, "adjacentCities": adjacentCities})
}

// AddAdjacentCityInTicket 往票里面添加经过的城市
func AddAdjacentCityInTicket(context *gin.Context) {
	var request requests.AddCityInTicketRequest
	if err := context.ShouldBindJSON(&request); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var city = models.CityModel{}
	var ticket = models.TicketModel{}
	global.Db.Take(&city, "city_name = ?", request.CityName)
	global.Db.Take(&ticket, request.TicketID)
	var cityTicket = models.TicketCityModel{
		CityID:      city.ID,
		TicketID:    ticket.ID,
		CityOrder:   request.CityOrder,
		TicketModel: ticket,
		CityModel:   city,
		Price:       request.Price,
		CityName:    request.CityName,
	}
	global.Db.Create(&cityTicket)
	context.JSON(http.StatusCreated, gin.H{"ticket": ticket})
	return
}
